//
//  SearchViewController.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/29.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "SearchViewController.h"
#import "baseTableview.h"
#import "SearchTableViewCell.h"
#import "APPTools.h"
#import "ModelOfSecondDetail.h"
#import "SearchDetailViewController.h"
@interface SearchViewController ()<UISearchBarDelegate,UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain)UISearchController *search;
@property (nonatomic,retain)baseTableview *tableview1;
@property (nonatomic,retain)NSString *str;
@property (nonatomic,retain)NSMutableArray *arrStr;
@property (nonatomic,retain)NSMutableArray *searchlist;

@end

@implementation SearchViewController

-(void)dealloc{
    [_arrStr release];
    [super dealloc];

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.arrStr = [NSMutableArray array];
    [self createTableView];
    [self createSearch];
   
    // Do any additional setup after loading the view.
}


#pragma mark - ------------数据处理  ---------------
-(void)handleData{
   
    //转码
    NSString * transcoding = [_str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:_str]];
    
    NSString *string = @"http://203.80.144.212/napi/blog/list/by_search/?screen_width=1080&app_version=74&start=0&locale=zh&kw=%E7%AB%8B%E5%A4%8F&screen_height=1920&platform_version=6.0.1&__domain=www.duitang.com&platform_name=Android&device_platform=MI%2B4LTE&include_fields=sender%2Calbum%2Cicon_url%2Creply_count%2Clike_count&app_code=nayutas";
    NSString *url = [string stringByReplacingOccurrencesOfString:@"kw=%E7%AB%8B%E5%A4%8F" withString:[NSString stringWithFormat:@"kw=%@",transcoding]];
    
    [APPTools GETWithURL:url par:nil success:^(id responseObject) {
        
       
        NSArray *arr = responseObject[@"data"][@"object_list"];
        
        self.arrStr = [ModelOfSecondDetail modelHandleWithArrar:arr];
        
       
        [_tableview1 reloadData];
        
    } filed:^(NSError *error) {

    }];

}



#pragma mark - ------------ 创建搜索视图 ---------------


-(void)createSearch{
        
        _search = [[UISearchController alloc]initWithSearchResultsController:nil];
        //搜索结果的视图控制器
    
        _search.searchResultsUpdater = self;
    
        //确定是否变暗在底层内容搜索。
    
        _search.dimsBackgroundDuringPresentation = NO;
        
        //是否隐藏导航栏
        _search.hidesNavigationBarDuringPresentation = NO;
        
        //设置大小
    
        _search.searchBar.frame = CGRectMake(self.search.searchBar.frame.origin.x, self.search.searchBar.frame.origin.y, 44, 44);
        
        self.tableview1.tableHeaderView = self.search.searchBar;
        
}


#pragma mark - ------------ 创建tableview ---------------
-(void)createTableView{
  
    
    _tableview1 = [[baseTableview alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _tableview1.delegate = self;
    _tableview1.dataSource = self;
    [self.view addSubview:_tableview1];
    
    [_tableview1 registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
}

#pragma mark - ------------ 返回行数 ---------------

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arrStr.count;
    
}


#pragma mark - ------------ cell赋值 ---------------

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    //如果搜索状态
    if (self.search.active && self.arrStr.count != 0) {
       
        ModelOfSecondDetail *m1 = self.arrStr[indexPath.row];
        
        NSString *prise = m1.path;
        
        //关键字范围
        
        NSRange range = [prise rangeOfString:_str];
        
        //转换成可操作的字符串类
        
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:prise];
        
        //设置关键字字体颜色并放大
        
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20 weight:3] range:range];
        
        //cell赋值
        
        [cell.textLabel setAttributedText:attStr];
        
        cell.textLabel.text = m1.msg;
        
        
        
    }
    
    return cell;
    
}
#pragma mark - ------------ search点击就执行的方法 ---------------

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    
    self.str = [self.search.searchBar text];
    [self handleData];
    
}

#pragma mark - ------------ 点击方法 ---------------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    SearchDetailViewController *vc = [[SearchDetailViewController alloc]init];
    
    ModelOfSecondDetail  *model = _arrStr[indexPath.row];
    
    vc.strDetail = model.iid.stringValue;
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
